package eu.liquify.ui {
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	public class Box extends UIContainer {
		protected static const HORIZONTAL:int = 0;
		protected static const VERTICAL:int = 1;
		
		protected static const ALIGN_LEFT:String = "left";
		protected static const ALIGN_CENTER:String = "center";
		protected static const ALIGN_RIGHT:String = "right";
		
		protected var boxType:int = HORIZONTAL;
		
		public var lines:Boolean = false;
		
		public var fitToGrid:Boolean = true;
		//public var fitToGrid:Boolean = false;
		//public var gridWidth:Number = 100;
		public var gridWidth:Number = 120;
		public var gridHeight:Number = 22;
		
		public var spacing:Number = 3;
		
		public var align:String = ALIGN_LEFT;
		
		protected var labelBox:VBox;
		
		protected var container:DisplayObjectContainer;
		
		protected var labelDisplay:Label;
		protected var contentContainer:DisplayObjectContainer;
		
		protected var items:Vector.<DisplayObject> = new Vector.<DisplayObject>();
		
		public function Box(options:Object = null) {
			super();
			fixedSize = false;
			applyOptions(options);
			container = content;
			
			mouseEnabled = false;
			content.mouseEnabled = false;
			//mouseChildren = false;
		}
		
		public function set type(v:String):void {
			if (v == "horizontal" || v == "h") boxType = HORIZONTAL;
			else if (v == "vertical" || v == "v") boxType = VERTICAL;
		}
		
		override public function set label(value:String):void {
			super.label = value;
			if (label == "") removeLabel();
			if (label != "") {
				addLabel();
				labelDisplay.label = label;
			}
		}
		
		private function addLabel():void {
			if (labelBox) return;
			
			contentContainer = container;
			removeRawChild(contentContainer);
			
			labelBox = new VBox();
			labelBox.addChildVector(new <DisplayObject>[
				labelDisplay = new TitleLabel( { fixedSize: true } ),
				container
			]);
			
			container = labelBox;
			addRawChild(container);
		}
		private function removeLabel():void {
			if (!labelBox) return;
			
			removeRawChild(labelBox);
			
			labelBox.removeChildVector(new <DisplayObject>[
				labelDisplay,
				contentContainer
			]);
			labelDisplay = null;
			labelBox = null;
			
			container = contentContainer;
			contentContainer = null;
			addRawChild(container);
		}
		
		override public function addChild(child:DisplayObject):DisplayObject {
			super.addChild(child);
			items.push(child);
			reposition();
			return child;
		}
		
		override public function removeChild(child:DisplayObject):DisplayObject {
			super.removeChild(child);
			items.splice(items.indexOf(child), 1);
			reposition();
			return child;
		}
		
		override public function resize():void {
			super.resize();
			for (var i:int = 0; i < items.length; i++) {
				var d:DisplayObject = items[i];
				if (d is UIItem) {
					var item:UIItem = d as UIItem;
					item.resize();
				}
			}
			reposition();
		}
		
		protected function reposition():void {
			var i:int;
			var w:Number = 0;
			var h:Number = 0;
			var max:Number = 0;
			
			var d:DisplayObject;
			
			var g:Graphics = content.graphics;
			g.clear();
			//g.lineStyle(1, 0xFF0000);
			//g.lineStyle(1, 0xFF0000);
			var coords:Vector.<Number> = new Vector.<Number>();
			
			for (i = 0; i < items.length; i++) {
				d = items[i];
				
				d.x = w;
				d.y = h;
				
				var item:UIItem = d as UIItem;
				if (item) {
					//item.fixedSize = fixedSize;
					if (autosizeCascade) {
						item.fixedSize = false;
						if (item is UIContainer) {
							(item as UIContainer).autosizeCascade = true;
						}
					}
					if (fitToGrid) {
						item.width = gridWidth;
						item.height = gridHeight;
					}
					item.resize();
				}
				
				if (boxType == HORIZONTAL) {
					w += (fitToGrid ? gridWidth : d.width)+spacing;
					if (d.height > max) max = d.height;
					if (lines) coords.push(w);
				} else {
					h += (fitToGrid ? gridHeight : d.height)+spacing;
					if (d.width > max) max = d.width;
					if (lines) coords.push(h);
				}
			}
		
			if (boxType == HORIZONTAL) {
				w -= spacing;
			} else {
				h -= spacing;
			}
			
			if (lines) {
				var m:Matrix = new Matrix();
				if (boxType == HORIZONTAL) {
					m.createGradientBox(1, max, Math.PI/2);
				} else {
					m.createGradientBox(max, 1);
				}
				//g.lineStyle(1);
				//g.lineGradientStyle(GradientType.LINEAR, [0x000000, 0x000000, 0x000000, 0x000000], [0, 0.2, 0.2, 0], [0, 50, 205, 255], m);
				//g.lineGradientStyle(GradientType.LINEAR, [0x000000, 0x000000, 0x000000], [0, 0.15, 0], [0, 127, 255], m);
				g.lineStyle(1, 0x000000, 0.1);
				for (i = 0; i < coords.length-1; i++) {
					var coord:Number = coords[i]-spacing/2;
					if (boxType == HORIZONTAL) {
						g.moveTo(coord, 0);
						g.lineTo(coord, max);
					} else {
						g.moveTo(0, coord);
						g.lineTo(max, coord);
					}
				}
			}
			
			if (!fixedSize && autosize) {
				if (boxType == HORIZONTAL) {
					//width = fitToGrid ? w : w-spacing;
					if (!fixedWidth) width = w;
					if (!fixedHeight) height = max;
					//height = fitToGrid ? gridHeight : max;
					//height = fitToGrid ? Math.max(max, gridHeight) : max;
				} else {
					//height = fitToGrid ? h : h-spacing;
					if (!fixedWidth) width = max;
					if (!fixedHeight) height = h;
					//width = fitToGrid ? gridWidth : max;
					//width = fitToGrid ? Math.max(max, gridWidth) : max;
				}
			}
			
			if (labelBox && !fixedHeight) height += labelBox.spacing+labelDisplay.height;
			updateLabel();
			
			switch (align) {
				case ALIGN_LEFT:
					container.x = 0;
					break;
				case ALIGN_CENTER:
					container.x = width/2-w/2;
					break;
				case ALIGN_RIGHT:
					container.x = width-w;
					break;
			}
			
			//redraw();
		}
		
		protected function updateLabel():void {
			if (labelBox) {
				labelDisplay.width = width;
				labelDisplay.resize();
			}
		}
		
		private function redraw():void {
			var g:Graphics = graphics;
			g.clear();
			g.beginFill(0x000000, 0.1);
			g.drawRect(0, 0, width, height);
		}
		
	}

}